//
//  AlarmTableCell.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/22.
//

import Foundation
import UIKit

class AlarmTableCell: UITableViewCell {
    let dateL = UILabel()
    let nameL = UILabel()
    let addrIV = UIImageView(image: UIImage(named: "ic_location2"))
    let addrL = UILabel()
    let statusL = UILabel()
    let arrowIV = UIImageView(image: UIImage(systemName: "chevron.right"))

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .bg
        setupUI()
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        if highlighted {
            layer.opacity = 0.6
        } else {
            layer.opacity = 1
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: AlarmsModal?) {
        let tips = Date.momentDate(modal?.tbAlarmTime ?? "")?.elTodayDate() ?? ""
        let text = modal?.tbAlarmTime ?? ""
        let attri = NSMutableAttributedString(string: text, attributes: [.font: UIFont.systemFont(ofSize: 15), .foregroundColor: UIColor.hex("#B0B0B0")])
        let range = NSMakeRange(text.distance(from: text.startIndex, to:text.range(of: tips)!.lowerBound), tips.count)
        attri.addAttribute(.foregroundColor, value: UIColor.black, range: range)
        dateL.attributedText = attri
        
        var typeText = "报警"
        var color: UIColor = .black
        switch AlarmType(rawValue: modal?.tbAlarmType ?? "") {
        case .robbery:
            typeText = "劫警"
            color = .hex("#ED9515")
        case .burglary:
            typeText = "盗警"
            color = .hex("#AA5FE5")
        case .fire:
            typeText = "火警"
            color = .hex("#329FD8")
        case .electricity:
            typeText = "电"
            color = .hex("#5456DE")
        case .shock:
            typeText = "震动"
            color = .hex("#7359E8")
        case .unalarm:
            typeText = "撤防"
            color = .hex("#7359E8")
        case .alarm:
            typeText = "布防"
            color = .hex("#7359E8")
        default:
            typeText = "报警"
            color = .hex("#FF0000")
            break
        }
        
        let type = "[\(typeText)]"
        let nameText = "\(type) \(modal?.tbDeviceName ?? "")\n\(modal?.tbDevicePosition ?? "")"
        
        let p = NSMutableParagraphStyle()
        p.lineSpacing = 8
        
        let nameAttri = NSMutableAttributedString(string: nameText, attributes: [.font: UIFont.systemFont(ofSize: 16), .foregroundColor: UIColor.black, .paragraphStyle: p])
        let typeRange = NSMakeRange(nameText.distance(from: nameText.startIndex, to:nameText.range(of: type)!.lowerBound), type.count)
        nameAttri.addAttribute(.foregroundColor, value: color, range: typeRange)
        nameL.attributedText = nameAttri
        
        addrL.text = modal?.tbWebsiteName
        statusL.text = ""
//        statusL.text = modal?.status == "1" ? "已处置" : "未处置"
//        statusL.textColor = modal?.status == "1" ? .hex("#BFBFBF") : .hex("#BFBFBF")
    }
    
    func setupUI() {
        let baseV = UIView()
        baseV.backgroundColor = .white
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
        }
        
        dateL.textColor = .hex("#B0B0B0")
        dateL.font = .systemFont(ofSize: 15)
        baseV.addSubview(dateL)
        dateL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(43)
            make.top.equalToSuperview().offset(10)
        }
        
        let contentV = UIView()
        contentV.backgroundColor = .hex("#F5F5F5")
        contentV.layer.cornerRadius = 10
        contentV.layer.masksToBounds = true
        baseV.addSubview(contentV)
        contentV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(43)
            make.top.equalTo(dateL.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.right.equalToSuperview().offset(-20)
        }
                                
        let dotV = UIView()
        dotV.backgroundColor = .hex("#D1D1D1")
        dotV.layer.cornerRadius = 6
        dotV.layer.masksToBounds = true
        baseV.addSubview(dotV)
        dotV.snp.makeConstraints { make in
            make.width.height.equalTo(12)
            make.left.equalToSuperview().offset(13)
            make.top.equalTo(contentV.snp.top).offset(15)
        }
        
        let line = UIView.createLine()
        line.backgroundColor = .hex("#F1F1F1")
        baseV.insertSubview(line, belowSubview: dotV)
        line.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.centerX.equalTo(dotV.snp.centerX)
            make.width.equalTo(1)
        }
        
        let addr = UIImageView(image: UIImage(named: "ic_left_arrow"))
        addr.contentMode = .scaleAspectFit
        baseV.addSubview(addr)
        addr.snp.makeConstraints { make in
            make.height.equalTo(15)
            make.right.equalTo(contentV.snp.left)
            make.centerY.equalTo(dotV.snp.centerY)
        }
        
        statusL.textColor = .hex("#BFBFBF")
        statusL.font = .systemFont(ofSize: 15)
        statusL.textAlignment = .right
        contentV.addSubview(statusL)
        statusL.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-12)
            make.width.equalTo(60)
        }
        
        nameL.textColor = .black
        nameL.font = .systemFont(ofSize: 16)
        nameL.numberOfLines = 0
        contentV.addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(statusL.snp.left)
            make.top.equalToSuperview().offset(14)
        }
        
        addrIV.contentMode = .scaleAspectFit
        contentV.addSubview(addrIV)
        addrIV.snp.makeConstraints { make in
            make.left.equalTo(nameL.snp.left)
            make.top.equalTo(nameL.snp.bottom).offset(14)
            make.height.equalTo(15)
            make.bottom.equalToSuperview().offset(-14)
        }
        
        addrL.textColor = .hex("#898989")
        addrL.font = .systemFont(ofSize: 16)
        contentV.addSubview(addrL)
        addrL.snp.makeConstraints { make in
            make.left.equalTo(addrIV.snp.right).offset(8)
            make.centerY.equalTo(addrIV.snp.centerY)
        }
        
        arrowIV.tintColor = .hex("#ABABAB")
        arrowIV.contentMode = .scaleAspectFit
        contentV.addSubview(arrowIV)
        arrowIV.snp.makeConstraints { make in
            make.right.equalTo(statusL.snp.right)
            make.centerY.equalTo(addrIV.snp.centerY)
            make.width.equalTo(10)
        }
    }
}
